package com.chuxing.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.chuxing.config.PermissionsState;
import com.chuxing.config.SecurityRole;
import com.chuxing.domain.DTO.AddGroupMembersByAdminDTO;
import com.chuxing.domain.DTO.NotGroupMembersDTO;
import com.chuxing.domain.DTO.TGroupMembersDTO;
import com.chuxing.domain.DTO.TGroupMembersListDTO;
import com.chuxing.domain.Result;
import com.chuxing.domain.TClassMembers;
import com.chuxing.domain.TGroupMembers;
import com.chuxing.domain.VO.PageResultVO;
import com.chuxing.exception.GenericException;
import com.chuxing.exception.UnauthorizedException;
import com.chuxing.system.service.ITGroupMembersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小组成员Controller
 * 
 * @author 刘祺
 * @date 2025-01-11
 */
@RestController
@RequestMapping("/system/groupMembers")
@Tag(name = "小组成员管理", description = "小组成员管理")
@Slf4j
@RequiredArgsConstructor
public class TGroupMembersController
{
    private final ITGroupMembersService tGroupMembersService;

    /**
     * 查询小组成员列表
     */
    @GetMapping("/list")
    @Operation(summary = "查询小组成员列表", description = "查询小组成员列表")
    public Result<PageResultVO> list(TGroupMembersListDTO tGroupMembersListDTO)
    {
        log.info("查询小组成员列表，参数为：{}", tGroupMembersListDTO);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        PageResultVO list = tGroupMembersService.selectTGroupmembersList(tGroupMembersListDTO);
        return Result.success( "查询小组成员列表成功",list);
    }

    /**
     * 导出小组成员列表
     */
    @Operation(summary = "导出小组成员列表", description = "导出小组成员列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody TGroupMembersListDTO tGroupMembersListDTO)
    {
        // 记录导出日志
        log.info("管理员导出小组成员列表");
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        // 检查用户是否具有管理员角色
        if (!StpUtil.hasRole(SecurityRole.ROLE_ADMIN)) {
            throw new GenericException(PermissionsState.PERMISSION_NOT_ENOUGH);
        }
        tGroupMembersService.export(response, tGroupMembersListDTO);
    }

    /**
     * 获取小组成员详细信息
     */
    @GetMapping(value = "/{GroupMemberID}")
    @Operation(summary = "获取小组成员详细信息", description = "获取小组成员详细信息")
    public Result<TGroupMembers> getInfo(@PathVariable("GroupMemberID") Long GroupMemberID)
    {
        log.info("获取小组成员详细信息，参数为：{}", GroupMemberID);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        // 检查用户是否具有管理员角色
        if (!StpUtil.hasRole(SecurityRole.ROLE_ADMIN)) {
            throw new GenericException(PermissionsState.PERMISSION_NOT_ENOUGH);
        }
        TGroupMembers tGroupMembers = tGroupMembersService.selectTGroupmembersByGroupMemberID(GroupMemberID);
        return Result.success( "获取小组成员信息成功",tGroupMembers);
    }

    /**
     * 新增小组成员
     */
    @PostMapping
    @Operation(summary = "用户加入小组", description = "用户加入小组")
    public Result<Object> add(@RequestBody TGroupMembersDTO tGroupMembersDTO)
    {
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        log.info("用户加入小组，参数为：{}：{}", tGroupMembersDTO,StpUtil.getLoginIdAsString());
        tGroupMembersService.insertTGroupmembers(tGroupMembersDTO.getGroupID());
        return Result.success( "加入小组成功");
    }

    /**
     * 修改小组成员
     */
    @PutMapping
    @Operation(summary = "管理员修改小组成员", description = "管理员修改小组成员")
    public Result<Object> edit(@RequestBody TGroupMembers tGroupmembers)
    {
        log.info("管理员修改小组成员，参数为：{}", tGroupmembers);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        // 检查用户是否具有管理员角色
        if (!StpUtil.hasRole(SecurityRole.ROLE_ADMIN)) {
            throw new GenericException(PermissionsState.PERMISSION_NOT_ENOUGH);
        }
        tGroupMembersService.updateTGroupmembers(tGroupmembers);
        return Result.success( "修改小组成员成功");
    }

    /**
     * 删除小组成员
     */
	@DeleteMapping("/{GroupMemberIDs}")
    @Operation(summary = "删除小组成员", description = "删除小组成员")
    public Result<Object> remove(@PathVariable Long[] GroupMemberIDs)
    {
        log.info("删除小组成员，参数为：{}", GroupMemberIDs);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        tGroupMembersService.deleteTGroupmembersByGroupMemberIDs(GroupMemberIDs);
        return Result.success( "删除小组成员成功");
    }

    /**
     * 未分组小组成员
     */
    @GetMapping("/NotGroupMembers")
    @Operation(summary = "未分组小组成员", description = "未分组小组成员")
    public Result<List<TClassMembers>> NotGroupMembers(NotGroupMembersDTO notGroupMembersDTO)
    {
        log.info("未分组小组成员，参数为：{}", notGroupMembersDTO);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        // 检查用户是否具有管理员角色
        if (!StpUtil.hasRole(SecurityRole.ROLE_ADMIN)) {
            throw new GenericException(PermissionsState.PERMISSION_NOT_ENOUGH);
        }
        List<TClassMembers> list = tGroupMembersService.selectNotGroupmembersList(notGroupMembersDTO);
        return Result.success( "获取未分组小组成员成功",list);
    }

    /**
     * 管理员分配成员小组
     */
    @PostMapping("/AddGroupMembersByAdmin")
    @Operation(summary = "管理员分配成员小组", description = "管理员分配成员小组")
    public Result<Object> AddGroupMembersByAdmin(@RequestBody AddGroupMembersByAdminDTO addGroupMembersByAdminDTO)
    {
        log.info("管理员分配成员小组，参数为：{}", addGroupMembersByAdminDTO);
        // 检查用户是否已登录
        if (!StpUtil.isLogin()) {
            throw new UnauthorizedException(PermissionsState.NOT_LOGIN);
        }
        // 检查用户是否具有管理员角色
        if (!StpUtil.hasRole(SecurityRole.ROLE_ADMIN)) {
            throw new GenericException(PermissionsState.PERMISSION_NOT_ENOUGH);
        }
        tGroupMembersService.AddGroupMembersByAdmin(addGroupMembersByAdminDTO);
        return Result.success( "分配成员小组成功");
    }
}
